// MetaDialog.cpp : implementation file
//

#include "stdafx.h"
#include "ejwork.h"
#include "MetaDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMetaDialog dialog


CMetaDialog::CMetaDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CMetaDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMetaDialog)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CMetaDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMetaDialog)
	DDX_Control(pDX, IDC_LIST_TMPL, m_lvTmpl);
	DDX_Control(pDX, IDC_EDIT_META, m_wndMeta);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMetaDialog, CDialog)
	//{{AFX_MSG_MAP(CMetaDialog)
	ON_LBN_SELCHANGE(IDC_LIST_TMPL, OnSelchangeListTmpl)
	ON_BN_CLICKED(ID_SAVETMPL, OnSavetmpl)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMetaDialog message handlers

BOOL CMetaDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();

	selIndex = -1;

	Refresh();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


void CMetaDialog::Refresh()
{
	WORD i;

	m_lvTmpl.ResetContent();

	for(i = 0;i<theApp.meta.num;i++)
	{
		m_lvTmpl.AddString(theApp.meta.item[i].title);	
	}

	if (m_lvTmpl.GetCount()>0)
	{
		m_lvTmpl.SetCurSel(0);
		OnSelchangeListTmpl();
	}

}

void CMetaDialog::OnSelchangeListTmpl() 
{
	CString str;
	int index;
	
	index = m_lvTmpl.GetCurSel();
	if (index>=0 && index<theApp.meta.num)
	{
		const CMetaTmplItem& ti = theApp.meta.item[index];
		ti.Output(str);
		m_wndMeta.SetWindowText(str);

		selIndex = index;
	}
}

void CMetaDialog::OnSavetmpl() 
{
	CString xml;
	CMetaSaveDialog dlg;

	dlg.title = theApp.ejd.meta.value[0];

	if (dlg.DoModal()==IDOK)
	{
		theApp.meta.Add(dlg.title,theApp.ejd.meta.name,theApp.ejd.meta.value);

		xml.Format("%s/meta.xml",theApp.baseDir);
		theApp.meta.SaveToXML(xml);

		Refresh();
	}
}
